//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.0/xslt/JavaClass.xsl

package com.crm.query;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.web.struts.DispatchActionSupport;

import com.crm.mydict.IMyDictDAO;

/** 
 * 
 * Creation date: ${datenow}
 * 
 * XDoclet definition:
 * @struts.action path="/usermanage" name="usermanageForm" input="/user/usermanage.jsp" scope="request" validate="true"
 */
public class QueryAction extends DispatchActionSupport {
	
	// --------------------------------------------------------- Instance Variables

	// --------------------------------------------------------- Methods

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward list(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		
		String sqlname=request.getParameter("sqlname");
		String sforward = request.getParameter("forward");
		
		Map params = genParams(request);
		
		ApplicationContext context = this.getWebApplicationContext();
		IQueryDAO dao = (IQueryDAO)context.getBean("querydao");
		
		List list = dao.getList(params, sqlname, request);
		request.setAttribute("list",list);
		
		return mapping.findForward(sforward);
	}
	
	private Map genParams(HttpServletRequest request){
		Enumeration paramnames = request.getParameterNames();
		Map params = new HashMap();
		while(paramnames.hasMoreElements()){
			String paramname = (String)paramnames.nextElement();
			if("task".equals(paramname)) continue;
			if("currentpage".equals(paramname)) continue;
			String[] paramvalues = request.getParameterValues(paramname);
			if(null!=paramvalues){
				if(1==paramvalues.length){
					params.put(paramname, paramvalues[0]);
				}else{
					StringBuffer sb = new StringBuffer();
					for(int i=0;i<paramvalues.length;i++){
						sb.append(",");
						sb.append(paramvalues[i]);
					}
					sb.deleteCharAt(0);
					params.put(paramname, sb.toString());
					
				}
				
			}
			
		
			
		}
		
		return params;
	}
	private void addTypeList(HttpServletRequest request){
		ApplicationContext context = this.getWebApplicationContext();
		IMyDictDAO dictdao = (IMyDictDAO)context.getBean("mydictdao");
	}
}

